'use strict';

const BaseController = require('./base');
const serviceName = 'major';

class MajorController extends BaseController {
  constructor(ctx) {
    super(ctx, serviceName);
  }

  getListQuery(query) {
    let where = {}
    if (query.lang) {
      where.lang = query.lang
    }
    if (query.school_id) {
      where.school_id = query.school_id
    }
    if (query.college) {
      // like
      where.college = {
        [this.app.Sequelize.Op.like]: `%${query.college}%`
      }
    }
    if (query.major) {
      // like
      where.major = {
        [this.app.Sequelize.Op.like]: `%${query.major}%`
      }
    }
    if (query.major_short) {
      // like
      where.major_short = {
        [this.app.Sequelize.Op.like]: `%${query.major_short}%`
      }
    }
    if (query.length) {
      // 大于等于min_length 小于等于max_length
      where.length = {
        [this.app.Sequelize.Op.between]: [query.min_length, query.max_length]
      }
      // 学费
      if (query.tuition) {
        // 小于等于
        where.tuition = {
          [this.app.Sequelize.Op.lte]: query.tuition
        }
      }
      if (query.status) {
        where.status = query.status
      }
      // apply_frequency
      if (query.apply_frequency) {
        where.apply_frequency = query.apply_frequency
      }
      // 申请时间，
      // if (query.start_time && query.end_time) {
      //   // 需要关联apply_date表，在该表中存在多个时间段
      //   where.applyDate = {
      //     [this.app.Sequelize.Op.between]: [query.start_time, query.end_time]
      //   }
      // }
      return where;
    }
  }

  async createList() {
    const { ctx } = this;
    const data = ctx.request.body;
    const res = await ctx.service[this.serviceName].createList(data);
    ctx.success(res);
  }
}

module.exports = MajorController;
